﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.GData.Calendar;

namespace Journal.Domain
{
    public class Event : ICloneable
    {
        public virtual Guid Id { get; set; }
        public virtual string Title { get; set; }
        public virtual string Description { get; set; }
        public virtual string Location { get; set; }
        public virtual Calendar Calendar { get; set; }
        public virtual DateTime StartTime { get; set; }
        public virtual DateTime EndTime { get; set; }
        public virtual bool AllDay { get; set; }

        // Following property is just for use in databinding in group box 'Event detail' in MainWindow
        public string When
        {
            get
            {
                if (StartTime != null && EndTime != null)
                {
                    string format = "";
                    string formatTime = "HH:mm";
                    if (this.AllDay) format = "dddd, dd.MM.yyyy";
                    else format = "dddd, dd.MM.yyyy HH:mm";

                    if (StartTime.DayOfYear.Equals(EndTime.DayOfYear))
                    {
                        if (this.AllDay) return StartTime.ToString(format);
                        else return StartTime.ToString(format) + " - " + EndTime.ToString(formatTime);
                    }
                    else
                    {
                        return StartTime.ToString(format) + " - " + EndTime.ToString(format);
                    }
                }
                return "";
            }
        }

        #region Google
        private EventEntry gevent = null;
        public EventEntry GoogleEvent
        {
            get { return gevent; }
            set
            {
                gevent = value;
                this.Title = gevent.Title.Text;
                this.Description = gevent.Content.Content;
                if (gevent.Times.Count > 0)
                {
                    this.StartTime = gevent.Times[0].StartTime;
                    this.EndTime = gevent.Times[0].EndTime;
                    this.AllDay = gevent.Times[0].AllDay;
                }
                if (gevent.Locations.Count > 0)
                    this.Location = gevent.Locations[0].ValueString;
                isGoogle = true;
            }
        }
        private bool isGoogle = false;
        public bool IsGoogleEvent
        {
            get
            {
                return this.isGoogle;
            }
        }
        #endregion

        public object Clone()
        {
            Event clone = new Event();
            clone.Title = this.Title;
            clone.Description = this.Description;
            clone.Location = this.Location;
            clone.Calendar = this.Calendar;
            clone.StartTime = this.StartTime;
            clone.EndTime = this.EndTime;
            clone.AllDay = this.AllDay;
            clone.gevent = this.gevent;
            return clone;
        }

        public bool IsSameAs(System.Object obj)
        {
            Event evnt = obj as Domain.Event;
            if ((object)evnt == null) return false;

            if (evnt.IsGoogleEvent) return this.gevent.Id.Equals(evnt.gevent.Id);
            else return this.Id.Equals(evnt.Id);
        }
    }
}
